package com.whnk.pm.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.whnk.pm.domain.AttendanceInfo;
import com.whnk.pm.domain.ImpSignInfo;
import com.whnk.pm.dto.SignExcelDTO;
import com.whnk.pm.mapper.AttendanceInfoMapper;
import com.whnk.pm.service.IAttendanceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.whnk.common.support.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * 考勤导入 服务层实现
 * 
 * @author whwhnk
 * @date 2020-01-08
 */
@Service
@Slf4j
public class AttendanceInfoServiceImpl implements IAttendanceInfoService
{
	@Autowired
	private AttendanceInfoMapper attendanceInfoMapper;

	/**
     * 查询考勤导入信息
     * 
     * @param id 考勤导入ID
     * @return 考勤导入信息
     */
    @Override
	public AttendanceInfo selectInfoById(Integer id)
	{
	    return attendanceInfoMapper.selectAttendanceInfoById(id);
	}
	
	/**
     * 查询考勤导入列表
     * 
     * @param info 考勤导入信息
     * @return 考勤导入集合
     */
	@Override
	public List<AttendanceInfo> selectInfoList(AttendanceInfo info)
	{
	    return attendanceInfoMapper.selectAttendanceInfoList(info);
	}
	
    /**
     * 新增考勤导入
     * 
     * @param info 考勤导入信息
     * @return 结果
     */
	@Override
	public int insertInfo(AttendanceInfo info)
	{
	    return attendanceInfoMapper.insertAttendanceInfo(info);
	}
	
	/**
     * 修改考勤导入
     * 
     * @param info 考勤导入信息
     * @return 结果
     */
	@Override
	public int updateInfo(AttendanceInfo info)
	{
	    return attendanceInfoMapper.updateAttendanceInfo(info);
	}

	/**
     * 删除考勤导入对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteInfoByIds(String ids)
	{
		return attendanceInfoMapper.deleteAttendanceInfoByIds(Convert.toStrArray(ids));
	}

	/**
	 * 导入考勤
	 *
	 * @param file
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void signExcelFileUpload(MultipartFile file) throws Exception {
		try (ExcelReader reader = ExcelUtil.getReader(file.getInputStream())) {
			List<Map<String, Object>> mapList = reader.read(7, 8, Integer.MAX_VALUE);
			Gson gson = new GsonBuilder()
					.setDateFormat("yyyy-MM-dd").create();
			List<SignExcelDTO> signExcelDTOList = mapList.stream()
					.map(k -> gson.fromJson(gson.toJson(k), SignExcelDTO.class)).collect(Collectors.toList());
			List<AttendanceInfo> attendanceInfoList = new ArrayList<>();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			signExcelDTOList.forEach(k ->
			{
				AttendanceInfo attendanceInfo = new AttendanceInfo();
				BeanUtils.copyProperties(k, attendanceInfo);
				attendanceInfo.setId(null);
				try {
					attendanceInfo.setSignStti(simpleDateFormat.parse(k.getSignStti()));
					attendanceInfo.setSignEdti(simpleDateFormat.parse(k.getSignEdti()));
				} catch (ParseException e) {
					log.error("format date error -> excel signDate", e);
				}
				attendanceInfoList.add(attendanceInfo);
			});
			Date date = new Date();
			for (AttendanceInfo attendanceInfo : attendanceInfoList) {
				AttendanceInfo attendanceInfoSelect = new AttendanceInfo();
				attendanceInfoSelect.setEmpId(attendanceInfo.getEmpId());
				attendanceInfoSelect.setSignDt(attendanceInfo.getSignDt());
				List<AttendanceInfo> impAttendanceInfoList = attendanceInfoMapper.selectAttendanceInfoList(attendanceInfoSelect);
				attendanceInfo.setImpTime(date);
				if (impAttendanceInfoList.size() > 0) {
					log.info("此用户今日考勤已导入， 更新考勤 " + attendanceInfo.getEmpId() + attendanceInfo.getSignDt());
					attendanceInfo.setId(impAttendanceInfoList.get(0).getId());
					attendanceInfoMapper.updateAttendanceInfo(attendanceInfo);
				} else {
					attendanceInfoMapper.insertAttendanceInfo(attendanceInfo);
				}
			}
		}
	}

	public List<String> getCompanyList(){
		List<String> companyList = attendanceInfoMapper.getCompanyAll();
		if (companyList == null) {
			companyList = new ArrayList<>();
		}
		companyList.add("母行");
		return companyList;
	}
}
